package 尊享面试.滑动窗口;

import java.util.LinkedList;
import java.util.Queue;

public class a1004最大连续1的个数 {
    static class Solution {
        // todo 答错了
        public int longestOnes(int[] nums, int k) {
            int len = nums.length;
            int i = 0;
            int j = 0;
            int cnt = 0;
            int ans = 0;
            Queue<Integer> queue = new LinkedList<>();
            while (j < len) {
                if (nums[j] == 0) {
                    cnt++;
                    if (cnt > k) {
                        Integer poll = queue.poll();
                        i = poll + 1;
                        cnt--;
                    }
                    queue.offer(j);
                }
                ans = Math.max(ans, j - i + 1);
                j++;
            }
            return ans;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1};
        int K = 3;
        System.out.println(solution.longestOnes(nums, K));

    }
}
